﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace zp.mk.CodeFinder
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>


    public partial class MainWindow : Window
    {
        List<FileInfo> filFiles = new List<FileInfo>();
        //List<String> slOut = new List<string>();

        public MainWindow()
        {
            InitializeComponent();
        }

        private async void btnOpen_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Task t1 = new Task(() => filFiles = doSomething(fbd.SelectedPath));
                t1.Start();

                await t1.ConfigureAwait(true);
                
                txtPath.Text = fbd.SelectedPath;
                ddTypes.IsEnabled = true;
                btnLookUp.IsEnabled = true;
                put((string)ddTypes.SelectedValue);
                //t1.Start();
            }
        }

        private List<FileInfo> doSomething(String path)
        {

            DirectoryInfo di = new DirectoryInfo(path);
            List<FileInfo> tmp;

            tmp = di.GetFiles().ToList<FileInfo>();
            foreach (DirectoryInfo di2 in di.GetDirectories("*", SearchOption.AllDirectories))
            {
                tmp.AddRange(di2.GetFiles());
            }

            return tmp;
        }

        private void ddTypes_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //put((string)e.AddedItems[0]);
            put((string)ddTypes.SelectedValue);
            //txtLookUp.Text = String.Empty;
        }

        private async void put(String FileType, String SearchString = null)
        {
            StringBuilder sb = new StringBuilder();
            foreach (FileInfo fi in filFiles)
            {
                if (fi.Extension == "." + FileType)
                {
                    if (SearchString != null && SearchString != String.Empty)
                    {
                        StreamReader sr = new StreamReader(fi.FullName);
                        string tmp = await sr.ReadToEndAsync();
                        if (tmp.Contains(SearchString))
                        {
                            sb.Append(fi.FullName + "\r\n");
                        }
                    }
                    else
                    {
                        sb.Append(fi.FullName + "\r\n");
                    }
                }
            }

            if (sb.Length > 0)
            {
                txtOut.Text = sb.ToString();
            }
            else if (txtOut != null)
            {

                txtOut.Text = String.Empty;
            }
        }

        private void btnLookUp_Click(object sender, RoutedEventArgs e)
        {
            put((String)ddTypes.SelectedValue, txtLookUp.Text);
        }
    }
}
